﻿using NaireSystem.Api;
using NaireSystem.Datas;
using NaireSystem.DBEntity;
using NaireSystem.Enums;
using NaireSystem.ViewModels;
using System.Collections.Generic;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using TYBase;
using TYControls;

namespace NaireSystem.Views
{
    /// <summary>
    /// ViewEditQt.xaml 的交互逻辑
    /// </summary>
    public partial class ViewNaireEditQt : TYWindow
    {
        ViewModelNaireEditQt vm = new ViewModelNaireEditQt();
        private readonly QtVO data;

        public ViewNaireEditQt(QtVO data)
        {
            InitializeComponent();
            this.DataContext = vm;
            this.data = data;
            this.Loaded += ViewEditQt_Loaded;
        }

        private async void ViewEditQt_Loaded(object sender, RoutedEventArgs e)
        {
            if (data != null)
            {
                vm.QtModel = UIDataHelper.DataAsUI(data);
                this.Title = "新增问卷题目";
            }
            else
            {
                this.Title = "编辑问卷题目";
            }
            await InitDictData();
        }




        //private void ComboBox_SelectionChanged(object sender, SelectionChangedEventArgs e)
        //{
        //    if (vm.SelectQtType == null || vm.QtModel == null)
        //    {
        //        return;
        //    }

        //    vm.QtModel.VisbleSelect = Visibility.Collapsed;
        //    vm.QtModel.VisbleShortAnswer = Visibility.Collapsed;

        //    if (vm.SelectQtType.Key == (int)EnumQtType.ShortAnswer)
        //    {
        //        vm.QtModel.VisbleShortAnswer = Visibility.Visible;
        //    }
        //    else
        //    {
        //        vm.QtModel.VisbleSelect = Visibility.Visible;
        //    }

        //    if (vm.SelectQtType.Key == (int)EnumQtType.SingleSelect
        //        || vm.SelectQtType.Key == (int)EnumQtType.TrueFalse)
        //    {
        //        var firstData = vm.QtModel.ListOption.FirstOrDefault(x => x.IsSelected == true);
        //        foreach (var item in vm.QtModel.ListOption)
        //        {
        //            item.IsSelected = false;
        //        }
        //        if (firstData != null)
        //        {
        //            firstData.IsSelected = true;
        //        }
        //    }

        //}


        private async void BtnClickOK(object sender, RoutedEventArgs e)
        {
            if (vm.SelectTag == null)
            {
                this.ShowMsg("请选择标签");
                return;
            }

            QtVO tblData = data;
            bool isEdit = data != null;
            if (data == null)
                tblData = new QtVO();
            tblData.score = vm.QtModel.Score;
            tblData.title = vm.QtModel.Title;
            tblData.examType = (int)EnumExamType.Naire;
            tblData.qtType = (int)EnumQtType.SingleSelect;
            tblData.tag = vm.SelectTag.Value;//设置标签

            List<string> listStr = new List<string>();
            //List<int> listSelect = new List<int>();
            for (int i = 0; i < vm.QtModel.ListOption.Count; i++)
            {
                var item = vm.QtModel.ListOption[i];
                if (item.Name.Null())
                {
                    this.ShowMsg("请不要填写空选项");
                    return;
                }
                listStr.Add(item.Name);
            }

            //tblData.listAnswer = listSelect;
            tblData.listOption = listStr;

            var ret = await Run(() =>
            {
                if (isEdit)
                {
                    return QtApi.UpdateQt(tblData);
                }
                else
                {
                    return QtApi.CreateQt(tblData);
                }
            });
            if (ret.code != 0)
            {
                this.ShowMsg(ret.msg);
                return;
            }

            this.DialogResult = true;


        }

        private void BtnClickAddOption(object sender, RoutedEventArgs e)
        {
            //if (vm.IsMYDChecked)
            //{
            //    this.ShowMsg("满意度调查不能改变");
            //    return;
            //}
            vm.QtModel.ListOption.Add(new OptionModel { IndexChar = (vm.QtModel.ListOption.Count).NumberToABC() });
        }

        private void BtnClickDelete(object sender, RoutedEventArgs e)
        {
            //if (vm.IsMYDChecked)
            //{
            //    this.ShowMsg("满意度调查不能改变");
            //    return;
            //}
            var dx = (sender as Button).DataContext as OptionModel;
            vm.QtModel.ListOption.Remove(dx);
            for (int i = 0; i < vm.QtModel.ListOption.Count; i++)
            {
                vm.QtModel.ListOption[i].Index = i;
            }
        }

        private void BtnClick_Check(object sender, RoutedEventArgs e)
        {

        }

        private async void BtnClickAddTag(object sender, RoutedEventArgs e)
        {
            ViewEditString view = new ViewEditString();
            if (view.ShowDialog() != true)
                return;
            string data = view.Data;
            {
                var ret = await Run(() =>
             {
                 return DictDataApi.AddItem(new Tbl_DictData
                 {
                     dict_type = "naire_tag_type",
                     dict_label = data
                 });
             });
                if (ret.code != 0)
                {
                    this.ShowMsg(ret.msg);
                    return;
                }
            }

            {
                await InitDictData();
            }

            //private void BtnClickDeleteTag(object sender, RoutedEventArgs e)
            //{
            //    var dx = (sender as Button).DataContext as TagModel;
            //    vm.QtModel.ListTag.Remove(dx);
            //}

            //private void BtnClickAddTag(object sender, RoutedEventArgs e)
            //{
            //    ViewEditString viewEdit = new ViewEditString();
            //    if (viewEdit.ShowDialog() == true)
            //    {
            //        string newData = viewEdit.Data;
            //        if (vm.QtModel.ListTag.Count(x => x.Name == newData) > 0)
            //        {
            //            this.ShowMsg($"标签({newData})已存在!");
            //            return;
            //        }
            //        vm.QtModel.ListTag.Add(new TagModel { Name = newData });
            //    }
            //}

            //private void BtnClick_Check(object sender, RoutedEventArgs e)
            //{
            //    var dx = (sender as CheckBox).DataContext as OptionModel;
            //    if (dx == null)
            //        return;
            //    if (vm.SelectQtType.Key == (int)EnumQtType.SingleSelect
            //        || vm.SelectQtType.Key == (int)EnumQtType.TrueFalse
            //        )
            //    {
            //        foreach (var item in vm.QtModel.ListOption)
            //        {
            //            item.IsSelected = false;
            //        }
            //        dx.IsSelected = true;
            //    }
            //}

            //private void BtnClickMYD(object sender, RoutedEventArgs e)
            //{
            //    if (vm.IsMYDChecked)
            //    {
            //        vm.QtModel.ListOption.Clear();
            //        vm.QtModel.ListOption.Add(new OptionItemModel { Title = "非常满意" });
            //        vm.QtModel.ListOption.Add(new OptionItemModel { Title = "满意" });
            //        vm.QtModel.ListOption.Add(new OptionItemModel { Title = "一般" });
            //        vm.QtModel.ListOption.Add(new OptionItemModel { Title = "不满意" });
            //        if (vm.QtModel.ListTag.Count(x => x.Name == "满意度调查") == 0)
            //        {
            //            vm.QtModel.ListTag.Add(new TagModel { Name = "满意度调查" });
            //        }
            //    }

            //}
        }

        private async Task InitDictData()
        {
            var ret = await Run(() =>
            {
                return DictDataApi.GetList(new Tbl_DictData
                {
                    dict_type = $"{EnumDictType.naire_tag_type}"
                });
            });
            if (ret.code != 0)
            {
                this.ShowMsg(ret.msg);
                return;
            }
            vm.ListTag.Clear();
            foreach (var item in ret.data)
            {
                IntStringData model = new IntStringData { Key = item.dict_value, Value = item.dict_label };
                vm.ListTag.Add(model);
                if (data != null)
                    if (item.dict_label == data.tag)
                    {
                        vm.SelectTag = model;
                    }
            }
        }
    }
}
